package cn.com.gridinfo.sooc.core.my.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.home.activity.HomeActivity;
import cn.com.gridinfo.sooc.core.my.order.fragment.OrderFragment;
import cn.com.gridinfo.sooc.utils.PagerAdapter;

/**
 * 我的订单
 * Created by 11299 on 2016/8/9.
 */
public class MyOrderActivity extends BaseSoocActivity {

    PagerAdapter adapter;//适配器
    String[] tabText = {"全部", "已付款", "待付款", "已取消"};// 选修卡文字
    public static TabLayout tabLayout;
    @InjectView(R.id.viewpager_myorder)
    ViewPager mViewPager;
    @InjectView(R.id.blank_icon)
    ImageView blankIcon;//空白页图标
    @InjectView(R.id.tipBlank)
    TextView tipBlank;//空白页提示文字
    public static RelativeLayout blankView;//空白页布局

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_order);
        ButterKnife.inject(this);
        SoocApplication.fromJoinPayResult = 2;
        tabLayout = (TabLayout) findViewById(R.id.sliding_tabs_order);
        tabLayout.setVisibility(View.GONE);
        //空白页元素的设置
        blankView = (RelativeLayout) findViewById(R.id.blank_view);
        tipBlank.setText("暂无订单");
        blankIcon.setImageResource(R.mipmap.blank_search);

        initView();
    }

    /**
     * 初始化界面数据
     */
    void initView() {
        adapter = new PagerAdapter(getSupportFragmentManager());

        for (int i = 0; i < tabText.length; i++) {
            adapter.addFragment(OrderFragment.newInstance(i + ""), tabText[i]);
            tabLayout.addTab(tabLayout.newTab().setText(tabText[i]));
        }
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(4);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    public String setupToolBarTitle() {
        return "我的订单";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        //从我的页面来直接返回；从首页-支付结果来，返回到我的，只有这两种来源
        String from = getIntent().hasExtra("from") ? getIntent().getStringExtra("from") : "";
        if (!from.equals("my")) {
            Intent intent = new Intent();
            intent.setClass(this, HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所要到的界面中间的activity
            startActivity(intent);
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        goBack();
    }
}
